﻿// Special effect for banner.
$(window).load(function () {
    var $bannerArea = $("#Banner");
    var options = { effect: "fold", pauseTime: 5000, directionNav: false, controlNav: false, keyboardNav: false, pauseOnHover: false };
    $bannerArea.nivoSlider(options);
});

function getPageLang() {
    var $language = $("meta[http-equiv=Content-Language]").attr("content");
    if (!$language) {
        $language = $("meta[httpEquiv=Content-Language]").attr("content"); // For IE6
    }
    switch ($language) {
        case "zh-tw":
            return "chi";
        case "zh-cn":
            return "chis";
        case "pt":
            return "por";
        case "en-gb":
            return "eng";
        default:
            return "chi";
    }
}

function insertSection() {
    var $bannerArea = $("#Banner");
    var newAreas = '<div id="News"></div><div id="Notice"></div><div id="Activities"></div><div class="spacer"></div>';
    $bannerArea.after(newAreas);
    $("#News").append("<h1></h1><p></p>");
    $("#Activities").append("<h1></h1><p></p>");
}

function parseNewsHeader(xml) {
    var $newsArea = $("#News");
    var lang = getPageLang();
    var title = $(xml).find('header[lang="' + lang + '"] title').text();
    var more = $(xml).find('header[lang="' + lang + '"] more').text();
    var moreHref = $(xml).find('header[lang="' + lang + '"] more').attr("link");
    var moreLink = '<a href="' + moreHref + '" target="_parent">' + more + "</a>";
    $newsArea.find("h1").html(title).end().find("p").html(moreLink);
}

function parseActsHeader(xml) {
    var $actsArea = $("#Activities");
    var lang = getPageLang();
    var title = $(xml).find('header[lang="' + lang + '"] title').text();
    var more = $(xml).find('header[lang="' + lang + '"] more').text();
    var moreHref = $(xml).find('header[lang="' + lang + '"] more').attr("link");
    var moreLink = '<a href="' + moreHref + '" target="_parent">' + more + "</a>";
    $actsArea.find("h1").html(title).end().find("p").html(moreLink);
}

function parseNewsList(xml) {
    var output = "<ul>";
    var item = "";
    var highlight = "";
    var $newsArea = $("#News");
    var lang = getPageLang();
    $(xml).find('article[type="release"]').each(function (i) {
        highlight = $(this).attr("highlight");
        if ($(this).find('item[lang=' + lang + '] > a').text() === "") {
            item = $(this).find('item[lang=' + lang + ']').text();
        }
        else {
            item = '<a href="';
            item += $(this).find('item[lang=' + lang + '] > a').attr("href");
            item += '" target="' + $(this).find('item[lang=' + lang + '] > a').attr("target") + '">';
            item += $(this).find('item[lang=' + lang + ']').text();
            item += '</a>';
        }
        if (i === 0) {
            if (highlight === "yes") {
                output += '<li class="first">' + item + '<span class="highlight"> </span></li>';
            }
            else {
                output += '<li class="first">' + item + '</li>';
            }
        }
        else {
            if (highlight === "yes") {
                output += '<li class="general">' + item + '<span class="highlight"> </span></li>';
            }
            else {
                output += '<li class="general">' + item + '</li>';
            }
        }
    });
    output += "</ul>";
    $newsArea.find("h1").after(output);
}

function parseNoticeList(xml) {
    var output = "<ul>";
    var item = "";
    var $noticeArea = $("#Notice");
    var lang = getPageLang();
    $(xml).find('article[type="notice"]').each(function (i) {
        item = $(this).find('item[lang=' + lang + '] > p').text();
        output += "<li>" + item + "</li>";
        $noticeArea.show();
    });
    output += "</ul>";
    $noticeArea.html(output);
}

function parseActsList(xml) {
    var output = "<ul>";
    var item = "";
    var $actsArea = $("#Activities");
    var lang = getPageLang();
    $(xml).find('article').each(function (i) {
        item = '<a href="';
        item += $(this).find('item[lang=' + lang + '] > a').attr("href");
        item += '" target="' + $(this).find('item[lang=' + lang + '] > a').attr("target") + '">';
        item += $(this).find('item[lang=' + lang + ']').text();
        item += '</a>';
        if (i === 0) {
            output += '<li class="first">' + item + '</li>';
        }
        else {
            output += '<li class="general">' + item + '</li>';
        }
    });
    output += "</ul>";
    $actsArea.find("h1").after(output);
}

$(document).ready(function () {
    // Special effect for services menu.
    var $servicesMenu = $("#Service").find("a");
    $servicesMenu.hover(function () {
        $(this).animate({ opacity: "0.8" }, 150);
    }, function () {
        $(this).animate({ opacity: "1" }, 150);
    });

    insertSection();

    var ranNum = Math.random();

    $.get("/xml/news/headers.xml?" + ranNum, {}, parseNewsHeader);
    $.get("/xml/activities/headers.xml?" + ranNum, {}, parseActsHeader);
    $.get("/xml/news/news_min.xml?" + ranNum, {}, parseNewsList);
    $.get("/xml/news/news_min.xml?" + ranNum, {}, parseNoticeList);
    $.get("/xml/activities/activities_min.xml?" + ranNum, {}, parseActsList);
});



